package com.example.Sao.MyActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.example.Sao.Adapter.MyFragmentPagerAdapter;
import com.example.Sao.Custom.MyViewPager;
import com.example.Sao.MyFragment.DeviceListFragment;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.example.Sao.Tools.SharedPrefsUtil;
import com.example.Sao.Tools.StatusColorUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class ReviseActivity extends MyBaseActivity {
    //ViewPagerFragment适配器
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    //
    private  Intent intent;
    //数据库帮助者
    private ObjectBoxHelper objectBoxHelper;
    //部门名称
    private String partment;
    //扫码模式选中状态
    private int choose = 0;
    //单选框
    private AlertDialog alertDialog2;
    //控件
    private FloatingActionButton floatbutton;
    private TextView check_center;
    private LinearLayoutCompat search;
    private AppCompatImageView iv_back;
    private TabLayout mTabLayout;
     private MyViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revise);
        //设置状态栏颜色
        StatusColorUtil.setColor(this, getResources().getColor(R.color.skyblue));
        //绑定控件
        initview();
        //初始化数据库使用者
        objectBoxHelper = ObjectBoxHelper.getInstance();
        //通过intent 获取部门
        intent  =getIntent();
        partment = intent.getStringExtra("partment");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
//        Log.d("Revise","回调onRestart");
//        check_center.setText("搜索与盘点"+partment+"的设备");
//        objectBoxHelper.RxQuery(partment, DeviceEntity_.useDepartment)
//                .subscribe(new Consumer<List<DeviceEntity>>() {
//                    @Override
//                    public void accept(List<DeviceEntity> deviceEntities) throws Exception {
//                        List<List<DeviceEntity>>e = new ArrayList<>();
//                        List<DeviceEntity>tr = new ArrayList<>();
//                        List<DeviceEntity>fs = new ArrayList<>();
//                        for (DeviceEntity deviceEntity:deviceEntities){
//                            if (deviceEntity.isStaus()){
//                                tr.add(deviceEntity);
//                            }else {
//                                fs.add(deviceEntity);
//                            }
//                        }
//                        e.add(deviceEntities);
//                        e.add(tr);
//                        e.add(fs);
//                        //初始化滑动界面
//                        initviewpager(e);
//                    }
//                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Revise","回调onResume");
        choose = 0;
        //设置搜索框标题
        check_center.setText("搜索"+partment+"的设备");
        //查询部门
        objectBoxHelper.RxQuery(partment, DeviceEntity_.useDepartment)
                .subscribe(new Consumer<List<DeviceEntity>>() {
                    @Override
                    public void accept(List<DeviceEntity> deviceEntities) throws Exception {
                        //全部设备数据列表
                        List<List<DeviceEntity>>e = new ArrayList<>();
                        //未盘点设备列表
                        List<DeviceEntity>tr = new ArrayList<>();
                        //已经盘点设备列表
                        List<DeviceEntity>fs = new ArrayList<>();
                        for (DeviceEntity deviceEntity:deviceEntities){
                            //根据设备是否被盘点进行分类
                            if (deviceEntity.isStaus()){
                                tr.add(deviceEntity);
                            }else {
                                fs.add(deviceEntity);
                            }
                        }
                        e.add(deviceEntities);
                        e.add(tr);
                        e.add(fs);
                        //myFragmentPagerAdapter不为空 刷新滑动界面
                        if (myFragmentPagerAdapter!=null) {
                            //初始化Fragment列表
                            List<Fragment> fragments = new ArrayList<>();
                            for (int i = 0; i < 3; i++) {
                                //Fragment列表加载Fragment
                                DeviceListFragment deviceListFragment = new DeviceListFragment();
                                deviceListFragment.setInfoBeanList(e.get(i));
                                fragments.add(deviceListFragment);
                            }
                            //ViewPagerFragment适配器设置Fragment列表
                            myFragmentPagerAdapter.setFragments(fragments, getSupportFragmentManager());
                            //设置Fragment适配器
                            mViewPager.setAdapter(myFragmentPagerAdapter);
                            //TabLayout和ViewPager绑定
                            mTabLayout.setupWithViewPager(mViewPager);
                            //设置TabLayout标题与ICON
                            mTabLayout.getTabAt(0).setText("全部");
                            mTabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_account_balance_wallet_red_24dp));
                            mTabLayout.getTabAt(1).setText("已盘点");
                            mTabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_chrome_reader_mode_red24dp));
                            mTabLayout.getTabAt(2).setText("未盘点");
                            mTabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_exposure_green_24dp));
                        }
                        //myFragmentPagerAdapter为空 就使用默认初始化
                        initviewpager(e);
                    }
                });
    }

    private void initview(){
        //绑定ID
        floatbutton = findViewById(R.id.floatbutton);
        check_center = findViewById(R.id.check_center);
        search  =findViewById(R.id.search);
        iv_back = findViewById(R.id.iv_back);
        mTabLayout = findViewById(R.id.list_item);
        mViewPager = findViewById(R.id.mViewPager);
        //设置点击监听回调
        floatbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取历史扫码模式选择
                String setting  =SharedPrefsUtil.getValue(ReviseActivity.this,"setting","");
                //判断是否为专业模式
                if (setting!=null&&"专业模式".equals(setting)) {
                    //启动专业扫码模式界面
                    Intent intent =  new Intent(ReviseActivity.this, StartActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("status",new String[]{"扫码盘点",partment});
                    intent.putExtras(bundle);
                    startActivity(intent);
                    overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
                }else {
                    //弹出选择框
                    showSingleAlertDialog();
                }
            }
        });
        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ReviseActivity.this,MainActivity.class));
                ReviseActivity.this.finish();
                overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
            }
        });
        //跳转到搜索界面
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  new Intent(ReviseActivity.this, SearchActivity.class);
                intent.putExtra("partment",partment);
                startActivity(intent);
                overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
            }
        });
    }

    private void initviewpager( List<List<DeviceEntity>>e){
        if (myFragmentPagerAdapter!=null){
            return;
        }
       myFragmentPagerAdapter =new MyFragmentPagerAdapter(getSupportFragmentManager());
       List<Fragment>fragments = new ArrayList<>();
        for (int i=0;i<3;i++){
            DeviceListFragment deviceListFragment = new DeviceListFragment();
            deviceListFragment.setInfoBeanList(e.get(i));
            myFragmentPagerAdapter.addFragment(deviceListFragment);
        }
        mViewPager.setAdapter(myFragmentPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setText("全部");
        mTabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_account_balance_wallet_red_24dp));
        mTabLayout.getTabAt(1).setText("已盘点");
        mTabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_chrome_reader_mode_red24dp));
        mTabLayout.getTabAt(2).setText("未盘点");
        mTabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_exposure_green_24dp));

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                break;
            case KeyEvent.KEYCODE_MENU:
                this.finish();
                break;
            case KeyEvent.KEYCODE_HOME:
                this.finish();
                break;
            case KeyEvent.KEYCODE_APP_SWITCH:
                this.finish();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**单选框
     *
     */
    public void showSingleAlertDialog(){
        String setting = SharedPrefsUtil.getValue(ReviseActivity.this,"setting","");
        final String[] items = {"专业模式", "普通模式"};
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("扫码模式选择");
        alertBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                choose = i;
            }
        });
        alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog2.dismiss();
                if ("普通模式".equals(items[choose])) {
                    Intent intent = new Intent(ReviseActivity.this, CaptureActivity.class);
                    intent.putExtra("partment", partment);
                    startActivity(intent);
                    overridePendingTransition(R.anim.anim_fade_in, R.anim.anim_fade_out);
                }else {
                    SharedPrefsUtil.putValue(ReviseActivity.this,"setting","专业模式");
                    Intent intent =  new Intent(ReviseActivity.this, StartActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("status",new String[]{"扫码盘点",partment});
                    intent.putExtras(bundle);
                    startActivity(intent);
                    overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
                }
            }
        });

        alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog2.dismiss();
            }
        });
        alertDialog2 = alertBuilder.create();
        alertDialog2.show();
    }
}
